package com.jtpay.unionpay.product.http;

import android.annotation.SuppressLint;
import android.content.Context;
import com.google.gson.JsonObject;
import com.jtpay.unionpay.product.R;
import com.jtpay.unionpay.product.http.dataStructure.BasicResponse;
import com.jtpay.unionpay.product.http.dataStructure.JsonParams;
import com.jtpay.unionpay.product.http.listener.TransApiResultListener;
import com.jtpay.unionpay.product.http.loader.ApiLoader;
import com.jtpay.unionpay.product.http.retrofit.RetrofitObserver;
import com.jtpay.unionpay.product.model.RemoteModel;
import com.jtpay.unionpay.product.utils.ApiConstant;
import com.jtpay.unionpay.product.utils.LogUtil;
import com.jtpay.unionpay.product.utils.manager.ToastUtil;

import java.math.RoundingMode;
import java.text.DecimalFormat;


public class ApiHelper {
    private Context mContext;
    private ApiLoader mApiLoader;
    private DecimalFormat decimalFormat = new DecimalFormat("0.00");

    private static class Holder {
        @SuppressLint("StaticFieldLeak")
        private static final ApiHelper mHelper = new ApiHelper();
    }

    public Context getContext() {
        return mContext;
    }

    private void setContext(Context context) {
        if (context != null) {
            mContext = context;
        }
        mApiLoader = new ApiLoader();
    }

    public static ApiHelper getInstance(Context context) {
        Holder.mHelper.setContext(context);
        return Holder.mHelper;
    }

    public void getTaskAPI(final TransApiResultListener<RemoteModel> listener) {
        JsonParams params = new JsonParams();
        mApiLoader.postHttpObserve(ApiConstant.GET_TASK_API, params)
                .subscribe(new RetrofitObserver<BasicResponse<JsonObject>>(mContext) {
                    @Override
                    public void onSuccess(BasicResponse<JsonObject> response) {
                        super.onSuccess(response);
                        LogUtil.log("getTaskAPI： " + response.toString());
                        try {
                            JsonObject object = response.getData();
                            RemoteModel model = new RemoteModel();
                            model.setOrderId(object.get("OrderId").getAsString());
                            String money = object.get("TradeMoney").getAsString();
                            decimalFormat.setRoundingMode(RoundingMode.DOWN);
                            money = decimalFormat.format(Double.parseDouble(money));
                            model.setTradeMoney(money);
                            listener.trans(model);
                        } catch (Exception e) {
                            e.printStackTrace();
                            ToastUtil.showShort(R.string.response_return_error);
                        }
                    }

                    @Override
                    public void onFail(BasicResponse<JsonObject> response) {
                        super.onFail(response);
                        getTaskAPI(listener);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getTaskAPI(listener);
                    }
                });
    }

    public void uploadQrInfoAPI(String OrderId, String QRContent, String SupplierId,
                                final TransApiResultListener<String> listener) {
        JsonParams params = new JsonParams();
        params.put("OrderId", OrderId);
        params.put("QRContent", QRContent);
        params.put("SupplierId", SupplierId);
        mApiLoader.postHttpObserve(ApiConstant.QR_INFO_API, params)
                .subscribe(new RetrofitObserver<BasicResponse<JsonObject>>(mContext) {
                    @Override
                    public void onSuccess(BasicResponse<JsonObject> response) {
                        super.onSuccess(response);
                        LogUtil.log("uploadQrInfoAPI： " + response.toString());
                        listener.trans("success");
                    }

                    @Override
                    public void onFail(BasicResponse<JsonObject> response) {
                        super.onFail(response);
                        listener.trans("failure");
                    }
                });
    }

    public void getUPOrderIdAPI(final TransApiResultListener<String> listener) {
        JsonParams params = new JsonParams();
        mApiLoader.postHttpObserve(ApiConstant.GET_UNION_PAY_ORDERID_API, params)
                .subscribe(new RetrofitObserver<BasicResponse<JsonObject>>(mContext) {
                    @Override
                    public void onSuccess(BasicResponse<JsonObject> response) {
                        super.onSuccess(response);
                        LogUtil.log("getUPOrderIdAPI： " + response.toString());
                        try {
                            listener.trans(response.getData().get("SupplierId").getAsString());
                        } catch (Exception e) {
                            e.printStackTrace();
                            ToastUtil.showShort(R.string.response_return_error);
                        }
                    }

                    @Override
                    public void onFail(BasicResponse<JsonObject> response) {
                        super.onFail(response);
                        getUPOrderIdAPI(listener);
                    }
                });
    }

}
